﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ahp;

namespace AhpTest
{
    [TestClass]
    public class AlternativeNodeTest
    {
        [TestMethod]
        public void InitialStateTest()
        {
            AlternativeNode alternativeNode = new AlternativeNode(new Alternative("Alternative"));
            Assert.AreEqual("Alternative", alternativeNode.Name);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void NameSetterTest()
        {
            AlternativeNode alternativeNode = new AlternativeNode(new Alternative("Alternative"));
            alternativeNode.Name = "New alternative";
        }

        [TestMethod]
        public void GlobalPriorityTest()
        {
            AlternativeNode singleAlternativeNode = new AlternativeNode(new Alternative());
            singleAlternativeNode.LocalPriority = 0.791M;

            Assert.AreEqual(0.791M, singleAlternativeNode.GlobalPriority);

            CriterionNode criterion = new CriterionNode("parent", 0.374M);
            AlternativeNode alternativeNode = new AlternativeNode(new Alternative(), 0.798M);
            criterion.AlternativeNodes.Add(alternativeNode);

            Assert.AreEqual(0.374M * 0.798M, alternativeNode.GlobalPriority);
        }

        [TestMethod]
        public void CriterionNodeSetterTest()
        {
            //Initial criterion setting
            AlternativeNode alternativeNode = new AlternativeNode(new Alternative());
            CriterionNode criterionNode = new CriterionNode();
            alternativeNode.CriterionNode = criterionNode;

            Assert.AreEqual(criterionNode, alternativeNode.CriterionNode);
            Assert.IsTrue(criterionNode.AlternativeNodes.Contains(alternativeNode));

            //Changing criterion
            CriterionNode newCriterionNode = new CriterionNode();
            alternativeNode.CriterionNode = newCriterionNode;

            Assert.AreEqual(newCriterionNode, alternativeNode.CriterionNode);
            Assert.IsFalse(criterionNode.AlternativeNodes.Contains(alternativeNode));
            Assert.IsTrue(newCriterionNode.AlternativeNodes.Contains(alternativeNode));

            //Setting null criterion
            alternativeNode.CriterionNode = null;

            Assert.AreEqual(null, alternativeNode.CriterionNode);
            Assert.IsFalse(newCriterionNode.AlternativeNodes.Contains(alternativeNode));
        }
    }
}
